package cn.tedu.egmvc3.base.exception;

import cn.tedu.egmvc3.base.response.JsonResult;
import cn.tedu.egmvc3.base.response.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler
    public JsonResult doHandleIllegalArgumentException(IllegalArgumentException ex){
        String data = ex.getMessage();
        log.error("doHandleIllegalArgumentException:" + data);

        return new JsonResult(StatusCode.OPERATION_FAILED,data);
    }

    /**
     * 2.第2个异常处理方法:可以处理所有RuntimeException及其子类异常的处理方法;
     */
    @ExceptionHandler
    public JsonResult doHandleRuntimeException(RuntimeException ex){
        String data = ex.getMessage();
        log.error("doHandleRuntimeException:" + data);

        return new JsonResult(StatusCode.OPERATION_FAILED, data);
    }

    /**
     * 3.第3个异常处理方法:可以处理所有异常的处理方法;
     */
    @ExceptionHandler
    public JsonResult doHandleThrowable(Throwable ex){
        String data = ex.getMessage();
        log.error("doHandleThrowable:" + data);

        return new JsonResult(StatusCode.THROWABLE_ERROR, data);
    }
}
